import {
  legacy_createStore,
  applyMiddleware,
} from 'redux'
import logger from 'redux-logger'
import thunk from 'redux-thunk'

import { ALLSTATE, ALLACTION,LOGNIT, SETLIST, ADDLIST, SETSHOPNUA,CHECKALLSHOP, CHECKHOOP } from '../types/store.d'

const initialState = {
  list: [],
  carlist: [],
  ordePrice: "",//定义总价
  asressList:[]
}

const reduer = (state: ALLSTATE = initialState, { type, payload }: ALLACTION) => {
  switch (type) {

    case SETLIST:
      state.list = payload;
      return { ...state }

    case ADDLIST:
      const newList = [...state.carlist]
      newList.push(payload)
      state.carlist = newList
      return { ...state }

    case CHECKHOOP:
      const shopList = [...state.carlist]
      shopList.forEach((item) => {
        if (item.id == payload.id) {
          item.isCheck = payload.value
        }
      })
      state.carlist = shopList
      return { ...state }

    case CHECKALLSHOP:
      const choseShoopList = [...state.carlist]
      choseShoopList.forEach((item) => {
        if(item.isCheck==true){
          item.isCheck =false
        }else{
          item.isCheck =true
        }
      })
      state.carlist = choseShoopList
      return { ...state }
    
      case SETSHOPNUA:
        const NewCarlist = [...state.carlist]
        NewCarlist.forEach((item,index)=>{
          if(item.isCheck==true){
            item.isCheck=false
          }else{
            item.isCheck=true
          }
        })
        state.carlist = NewCarlist
        return { ...state }
        
      case SETSHOPNUA:
        const LOGNIT = [...state.asressList]
        LOGNIT.push(payload)
        state.asressList = LOGNIT
        return { ...state }


    default:
      return state
  }
}

const store = legacy_createStore(reduer, applyMiddleware(logger, thunk))

export default store